package com.chookss.home.audit.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.audit.project.GroupDetailActivity;
import com.chookss.home.audit.reimbursement.ReimbursementDetailActivity;
import com.chookss.home.entity.WorkNewEntity;
import com.chookss.tools.ClearMoreZeroUtil;
import com.chookss.tools.MyEvent;
import com.chookss.tools.Utils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReimbursementRecordAdapter extends BaseQuickAdapter<WorkNewEntity, BaseViewHolderEx> {
    private Activity context;
    private int type = 7; //7报销记录  9借款记录10补贴记录11用章记录

    public ReimbursementRecordAdapter(int layoutResId, Activity context, @io.reactivex.annotations.Nullable List<WorkNewEntity> data, int type) {
        super(layoutResId, data);
        this.context = context;
        this.type = type;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, WorkNewEntity entity) {
        TextView tvTitle = helper.getView(R.id.tvTitle);
        TextView tvTitleType = helper.getView(R.id.tvTitleType);
        TextView tvMoney = helper.getView(R.id.tvMoney);
        TextView tvTime = helper.getView(R.id.tvTime);
        TextView tvPost = helper.getView(R.id.tvPost);
        TextView tvGroup = helper.getView(R.id.tvGroup);
        TextView tvInit = helper.getView(R.id.tvInit);
        LinearLayout llMoney = helper.getView(R.id.llMoney);
        LinearLayout llGroup = helper.getView(R.id.llGroup);
        LinearLayout llPost = helper.getView(R.id.llPost);
        tvTitle.setText(entity.getProjectName());
        tvTime.setText("发起时间："+entity.getCreateTime());
        if (entity.getCacheStatus() == 2) {
            if (Utils.isNull(entity.getErrorRemark())) {
                tvInit.setText("无");
            } else {
                tvInit.setText(entity.getErrorRemark());
            }
            if (Utils.isNull(entity.getErrorGroupName())) {
                llGroup.setVisibility(View.GONE);
            } else {
                llGroup.setVisibility(View.VISIBLE);
                tvGroup.setText("所属项目组：" + entity.getErrorGroupName());
            }
            if (Utils.isNull(entity.getErrorOrgName())) {
                llPost.setVisibility(View.GONE);
            } else {
                llPost.setVisibility(View.VISIBLE);
                tvPost.setText("所属部门：" + entity.getErrorOrgName());
            }
            if (type == 7) {
                tvMoney.setText("报销金额：" + ClearMoreZeroUtil.subZeroAndDot(entity.getErrorAmt()) + "元");
            } else if (type == 10) {
                tvMoney.setText("补贴金额：" + ClearMoreZeroUtil.subZeroAndDot(entity.getErrorAmt()) + "元");
            } else {
                tvMoney.setText("借款金额：" + ClearMoreZeroUtil.subZeroAndDot(entity.getErrorAmt()) + "元");
            }
            tvTitleType.setText("上传失败");
            tvTitleType.setTextColor(context.getResources().getColor(R.color.main_color));
            tvTitleType.setBackgroundResource(R.drawable.shape_main_color_4_1f);
        } else {
            if (type == 7) {
                tvMoney.setText("报销金额：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getAmt()) + "元");
            } else if (type == 10) {
                tvMoney.setText("补贴金额：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getAmt()) + "元");
            } else {
                tvMoney.setText("借款金额：" + ClearMoreZeroUtil.subZeroAndDot(entity.getParams().getAmt()) + "元");
            }
            if (!Utils.isNull(entity.getParams().getRepaymentRemark())) {
                tvInit.setText(entity.getParams().getRepaymentRemark());
            } else if (!Utils.isNull(entity.getParams().getRemark())) {
                tvInit.setText(entity.getParams().getRemark());
            } else {
                tvInit.setText("无");
            }
            if (Utils.isNull(entity.getParams().getGroupName())) {
                llGroup.setVisibility(View.GONE);
            } else {
                llGroup.setVisibility(View.VISIBLE);
                tvGroup.setText("所属项目组：" + entity.getParams().getGroupName());
            }
            if (Utils.isNull(entity.getParams().getOrgName())) {
                llPost.setVisibility(View.GONE);
            } else {
                llPost.setVisibility(View.VISIBLE);
                tvPost.setText("所属部门：" + entity.getParams().getOrgName());
            }
            if ("0".equals(entity.getAuditStatus())) {

                if ("9".equals(entity.getWorkFlowType()) && "1".equals(entity.getParams().getRepaymentCd())) {
                    tvTitleType.setText("还款中");
                } else {
                    tvTitleType.setText("审批中");
                }
                tvTitleType.setTextColor(Color.parseColor("#FCA817"));
                tvTitleType.setBackgroundResource(R.drawable.shape_fca817_4_1f);
            } else if ("1".equals(entity.getAuditStatus())) {
                if ("9".equals(entity.getWorkFlowType())) {
                    if ("1".equals(entity.getParams().getRepaymentCd())) {
                        tvTitleType.setText("还款中");
                        tvTitleType.setTextColor(Color.parseColor("#FCA817"));
                        tvTitleType.setBackgroundResource(R.drawable.shape_fca817_4_1f);
                    } else if ("2".equals(entity.getParams().getRepaymentCd())) {
                        tvTitleType.setText("已还款");
                        tvTitleType.setTextColor(Color.parseColor("#9A9A9A"));
                        tvTitleType.setBackgroundResource(R.drawable.shape_e5e5e5_4);
                    } else {
                        tvTitleType.setText("已通过");
                        tvTitleType.setTextColor(Color.parseColor("#44D3B3"));
                        tvTitleType.setBackgroundResource(R.drawable.shape_44d3b3_4_2f);
                    }
                } else {
                    tvTitleType.setText("已通过");
                    tvTitleType.setTextColor(Color.parseColor("#44D3B3"));
                    tvTitleType.setBackgroundResource(R.drawable.shape_44d3b3_4_2f);
                }

            } else {
                if ("3".equals(entity.getAuditStatus())) {
                    tvTitleType.setText("废弃");
                } else {
                    tvTitleType.setText("不通过");
                }
                tvTitleType.setTextColor(Color.parseColor("#F2434D"));
                tvTitleType.setBackgroundResource(R.drawable.shape_f2434d_4_2f);
            }
        }
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                Intent intent = null;
                if (entity.getCacheStatus() == 2) {
                    //上传失败
                    Map<String, String> map = new HashMap<>();
                    map.put("ClockInAllRecordActivity_error_position", helper.getItemPosition() + "");
                    EventBus.getDefault().post(new MyEvent(map));
                } else {
                    if (type == 7||type == 9||type == 10) {
                        intent = new Intent(context, ReimbursementDetailActivity.class);
                        intent.putExtra("data", entity);
                        intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
                        context.startActivity(intent);
                    } else if (type == 16) {
                        intent = new Intent(context, GroupDetailActivity.class);
                        intent.putExtra("repairCode", entity.getProjectCode());
                        intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
                    }
                }
            }
        });
    }
}
